Code Question in Python Programming for Finance Part 10
In Python Programming for Finance Part 10 there is the following helper function used to create tags to buy, sell, or hold a stock.
def buy_sell_hold(*args): cols = [c for c in args] requirement = 0.02 for col in cols: if col > requirement: return 1 if col < -requirement: return -1 return 0
The call to the function is made elsewhere using a mapping function: You pass in 7 arguments.
1. Can you please explain this line in the helper function:
cols = [c for c in args]
It looks like each row passed into the function is a list of 7 values. I'm not sure how this line of code "translates" list elements into columns for interpretation by the function.
2. A more general question. For each ticker passed in does the function make the buy/sell label determination based on the first value it finds that is > .02 / < -.02? If that is the case I'm unclear how the function "knows" how to disregard subsequent values that may exceed the .02 requirement. So if the function, for a particular ticker, on a particular date, finds that on Day 3 vs Day 0 change was >.02 and the Day 5 vs Day 0 change was <.02, the tag will be based on the Day 3 signal and the Day 5 signal will somehow be disregarded? I'm a bit confused.
Thanks in advance.
You must be logged in to post. Please login or register an account.
1. cols = [c for c in args]
First, we need to understand what args are. Args are special, and come from the *args parm of this helper function. *args let us pass an indeterminate # of arguments to a function. *kwargs is the other option, which lets us pass keyword args.
You can learn more about args and kwargs here: https://pythonprogramming.net/args-kwargs-intermediate-python-tutorial/
Then the whole [c for c in args] is list comprehension. You can use list comprehension to either do something in a short for loop on one line, or you can use it to create an actual list. In our case, we're creating a list. We also could just skip the creation of the list in the first place, and just iterate over the args. I made the list since I wanted to leave more options open for this function, but it wound up not being totally necessary.
You can learn more about list comprehension here: https://pythonprogramming.net/list-comprehension-generators-intermediate-python-tutorial/
2. A more general question. For each ticker passed in does the function make the buy/sell label determination based on the first value it finds that is > .02 / < -.02?
Yes.
This is because a return is breaking/exiting for a function, so once a return statement is reached, that's it for the function.
There are other types of blocks of code you can write that, rather than a single return, can produce streams of returns, called generators, and generators are introduced in that tutorial linked above (https://pythonprogramming.net/list-comprehension-generators-intermediate-python-tutorial/), and continue to part 5, then are further explained in part 9: https://pythonprogramming.net/more-generators-intermediate-python-tutorial/
Hope all this helps!
-Harrison 8 years ago
You must be logged in to post. Please login or register an account.
Thanks Harrison. The problem was that I didn't understand list comprehensions. Very helpful.
-mksamelson 8 years ago
You must be logged in to post. Please login or register an account.